// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::Manager;
use window_shadows::set_shadow;

use crate::{
    api::TauriMP4GameApi,
    setup::{add_setup_task, TauriMP4GameSetup},
    state::AppState,
    tray::TauriMP4GameTray,
    util::set_window_materia,
};

mod api;
mod config;
mod event;
mod setup;
mod state;
mod tray;
mod util;

#[tokio::main]
async fn main() {
    #[cfg(debug_assertions)]
    const LOG_LEVEL: &str = "debug";

    #[cfg(not(debug_assertions))]
    const LOG_LEVEL: &str = "error";

    // 初始化日志系统
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(LOG_LEVEL))
        .target(env_logger::Target::Stdout)
        .init();

    // 获取并打印系统信息
    let osi = os_info::get();
    log::info!("系统信息：{:?}", osi);

    // 添加窗口阴影、材质等显示效果
    add_setup_task(|app| {
        app.get_window("main")
            .ok_or("找不到 main 窗口".to_string())
            // 设置窗口阴影
            .and_then(|window| {
                set_shadow(&window, true)
                    .map(|_| window)
                    .map_err(|e| e.to_string())
            })
            // 添加窗口材质
            .and_then(|window| set_window_materia(&window, None))
            .expect("窗口显示效果初始化失败");
        Ok(())
    })
    .expect("窗口显示效果初始化失败：初始化任务添加失败");

    // 构建 Tauri 应用
    tauri::Builder::default()
        .manage(AppState::default())
        .invoke_mp4game_tray()
        .invoke_mp4game_handler()
        .setup_mp4game()
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
